package com.catersystem.zmy.catersystem.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.catersystem.zmy.catersystem.R;
import com.catersystem.zmy.catersystem.base.BaseFragment;
import com.catersystem.zmy.catersystem.utils.AudioUtils;
import com.catersystem.zmy.catersystem.utils.DialogUtil;
import com.catersystem.zmy.catersystem.utils.KeyboardUtils;
import com.catersystem.zmy.catersystem.utils.SPUtils;
import com.catersystem.zmy.catersystem.utils.ScreenUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 语音呼叫
 * Created by Administrator on 2017/10/31 0031.
 */

public class VoiceFragment extends BaseFragment {

    @BindView(R.id.tv_try_listen)
    TextView mTvTryListen; //试听
    @BindView(R.id.tv_play_num)
    TextView mTvPlayNum;//播报次数
    @BindView(R.id.rb_call_out_table)
    RadioButton mRbCallOutTable;
    @BindView(R.id.rb_call_out_order)
    RadioButton mRbCallOutOrder;
    @BindView(R.id.rb_un_call_out)
    RadioButton mRbUnCallOut;
    @BindView(R.id.rg_call_out)
    RadioGroup mRgCallOut;

    private Dialog mDialogVoiceTime;//播报次数选择弹窗
    private View mVoiceTimeView;//播报次数布局
    private RadioGroup mRgVoiceTime;//播报次数选择
    private EditText mEtVoiceTime;//自定义设置播报次数
    private ImageView mIvCancel1;
    private TextView mTvSave;
    private TextView mTvCancel;
    private String mVoiceTime;//播报次数
    private int mCallOut = 1;

    @Override
    protected View initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle
            savedInstanceState) {
        return inflater.inflate(R.layout.fragment_setting_voice, container, false);
    }

    @Override
    protected void initView(View view) {
        AudioUtils.getInstance().init(activity); //初始化语音对象
        //默认播放次数
        mTvPlayNum.setText(SPUtils.getInstance().getString("voice_time") == "" ? "3" : SPUtils.getInstance().getString
                ("voice_time"));

        findDialogVoiceTime();
        if (mDialogVoiceTime == null) {
            mDialogVoiceTime = DialogUtil.getDialog1(activity, true, mVoiceTimeView, ScreenUtil.getScreenWidthPix
                    (activity) * 0.5, ScreenUtil.getScreenHeightPix(activity) * 0.4);
        }
        mRbCallOutTable.setChecked(true);
        mRgCallOut.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_call_out_table:
                        mCallOut = 1;
                        break;
                    case R.id.rb_call_out_order:
                        mCallOut = 2;
                        break;
                    case R.id.rb_un_call_out:
                        mCallOut = 0;
                        break;
                }
                SPUtils.getInstance().put("call_out", mCallOut);
            }
        });
    }


    /**
     * 菜品超时预警控件实例化
     */
    private void findDialogVoiceTime() {
        mVoiceTimeView = View.inflate(activity, R.layout.dialog_voice_time, null);
        mRgVoiceTime = (RadioGroup) mVoiceTimeView.findViewById(R.id.rg_voice_time);
        mEtVoiceTime = (EditText) mVoiceTimeView.findViewById(R.id.et_voice_time);
        mIvCancel1 = (ImageView) mVoiceTimeView.findViewById(R.id.iv_cancel);
        mTvSave = (TextView) mVoiceTimeView.findViewById(R.id.tv_save);
        mTvCancel = (TextView) mVoiceTimeView.findViewById(R.id.tv_cancel);
        mTvSave.setOnClickListener(mOnClickListener);
        mTvCancel.setOnClickListener(mOnClickListener);
        mIvCancel1.setOnClickListener(mOnClickListener);
        mEtVoiceTime.setOnClickListener(mOnClickListener);

        //默认取消输入框的焦点
        hideFocus(mEtVoiceTime);
        //分钟选择
        mRgVoiceTime.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_one_time://1次
                        mVoiceTime = "1";
                        break;
                    case R.id.rb_three_time://3次
                        mVoiceTime = "3";
                        break;
                    case R.id.rb_five_time://5次
                        mVoiceTime = "5";
                        break;
                    case R.id.rb_ten_time://10次
                        mVoiceTime = "10";
                        break;
                    case R.id.rb_fifteen_time://15次
                        mVoiceTime = "15";
                        break;
                }
                //取消焦点
                hideFocus(mEtVoiceTime);
                KeyboardUtils.hideSoftInput(mRgVoiceTime);
            }
        });
    }

    /**
     * 取消焦点
     */
    private void hideFocus(View view) {
        view.setFocusable(false);
        view.setFocusableInTouchMode(false);
        view.clearFocus();
    }

    /**
     * 菜品超时时间弹窗点击事件
     */
    View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            KeyboardUtils.hideSoftInput(v);
            switch (v.getId()) {
                case R.id.et_voice_time:
                    mRgVoiceTime.clearCheck();//取消选中
                    //获取焦点
                    mVoiceTime = "";
                    KeyboardUtils.showSoftInput(mEtVoiceTime);
                    break;
                case R.id.tv_save://保存
                    if (!TextUtils.isEmpty(mEtVoiceTime.getText().toString())) {
                        mVoiceTime = mEtVoiceTime.getText().toString().trim();
                    }
                    if (TextUtils.isEmpty(mVoiceTime)) {
                        showShortToast("请选择语音播报次数！");
                        return;
                    }
                    mTvPlayNum.setText(mVoiceTime);
                    AudioUtils.getInstance().setCount(Integer.parseInt(mVoiceTime));
                    mDialogVoiceTime.dismiss();
                    SPUtils.getInstance().put("voice_time", mVoiceTime);
                    break;
                case R.id.tv_cancel://取消
                case R.id.iv_cancel:
                    mDialogVoiceTime.dismiss();
                    break;
            }
        }
    };


    @OnClick({R.id.tv_try_listen, R.id.tv_play_num})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_try_listen://试听-默认女声
                AudioUtils.getInstance().speakText("全城淘信息技术服务有限公司"); //播放语音
                break;
            case R.id.tv_play_num://选择播放次数
                mDialogVoiceTime.show();
                mRgVoiceTime.clearCheck();
                mEtVoiceTime.setText("");
                break;
        }
    }
}
